#This file generates minimal number of columns only. The columns are now sorted based on the actual feature number. See usage of %hVars_sorted.

my $inFileName = "C:/Users/Mittal/Documents/Work/Code/survival-analysis/perl/NTDBNEW_Imputation_Train_LD_imputed_0.txt";
my $outFileName = '> C:/Users/Mittal/Documents/Work/Code/survival-analysis/perl/NTDBNEW_Imputation_Train_LD_imputed_0_csv.txt';
open(IN, $inFileName);
open (OUT, $outFileName);
print OUT "header\n";
my $count = 1;
my %hVars;
my $maxCol = 1;
#filling hash
while(<IN>) {
    chomp;
    @t = split " ";
    @t1  = split ":",$t[0];
    my ($i,$j);
    
    for ($i = 1; $i < @t; $i++)
    {
	@t1  = split ":",$t[$i];
	$col = int($t1[0]);
	if(!exists($hVars{$col})){
	    $hVars{$col} = $maxCol;
	    $maxCol++;
	}
    }
}
print "Number of unique vars = " . keys(%hVars);
close(IN);

open(IN, $inFileName);

my @aKeys = sort {$a <=> $b} keys(%hVars);
my %hVars_sorted;
@hVars_sorted{@aKeys} = 0..$#aKeys;
while(<IN>) {
    chomp;
    @t = split " ";
    @t1  = split ":",$t[0];
    
    my @aVars = (0) x keys(%hVars);
    

    my $line;
#    $line = $line . "$count";
    $count++;
    
    $line = $line . "$t1[0],$t1[1]";
    
    for ($i = 1; $i < @t; $i++)
    {
	@t1  = split ":",$t[$i];
	$col = int($t1[0]);
        $aVars[$hVars_sorted{$col}] = $t1[1];
    }
    my $aLine = join(',', @aVars);
    if(length($aLine) > 0)
    {
        $line = $line . ",$aLine";
    }
    print OUT "$line\n";
}
close(OUT);
close(IN);